'''
内置函数
from pprint import pprint
# 格式化输出的库
pprint(dir(__builtins__))
'''
# 常见的内置函数
# str
'''
>>> int(1.1)
1
>>> int("1.0")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1.0'
>>> int("2")
2

'''
# float
'''
>>> float("1.00")
1.0
>>> float("1")
1.0
>>> float("1.1")
1.1

'''
# bytes
'''
>>> bytes("ab".encode("utf-8"))
b'ab'

'''
# bool
'''
>>> bool(1)
True
>>> bool(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> bool(1)
True
>>> bool('1')
True
>>> bool('0')
True
>>> bool(0)
False
>>> bool(0.0)
False
>>> bool(0.1)
True
>>> bool("0")
True
>>> bool("a")
True

'''
# list
'''
>>> list("1234567")
['1', '2', '3', '4', '5', '6', '7']
>>> list("abcdef")
['a', 'b', 'c', 'd', 'e', 'f']

'''
# tuple
'''
>>> tuple("ab123")
('a', 'b', '1', '2', '3')

'''
# dict
'''
>>> dict(a=2)
{'a': 2}
>>> dict(a=2,b=1)
{'a': 2, 'b': 1}

'''
# set
'''
>>> set([1,3,5,7,9])
{1, 3, 5, 7, 9}
>>> set('abcdef')
{'e', 'd', 'c', 'a', 'b', 'f'}

'''
# id
'''
>>> a = "2"
>>> id(a)
51010400

'''
# dir
'''
 >>> dir(__builtins__)
['ArithmeticError', 

'''
# max
'''
>>>> max([1,3,5,7,9])
9

'''
# min
'''
>>> min([1,3,5,7,9])
1

'''
# range
'''
>>> range(100)
range(0, 100)
>>> r = range(100)
>>> r
range(0, 100)
>>> l = []
>>> for i in range(10):
...     l.append(i)
...
>>> print(l)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

'''
# 函数的形参和实参
# 形參

def my_power(a, b):
    return a ** b
# 实参
print(my_power(2,3))

# 函数的返回值
'''
>>> def foo(a,b):
...     return a*2,b*2
...
>>> result = foo(2,3)
>>> result
(4, 6)
>>>
>>> a,b = foo(2,3)
>>> a
4
>>> b
6
>>> result = foo(2,3)
>>> a,b = result
>>> a
4
>>> b
6
>>> result
(4, 6)
>>> result = (2,4,6,8,0)
>>> result
(2, 4, 6, 8, 0)
>>> a,*b,c = result
>>> a
2
>>> b
[4, 6, 8]
>>> c
0
>>> a,*b,c,d = result
>>> a
2
>>> b
[4, 6]
>>> c
8
>>> d
0

'''
# 匿名函数

'''
>>> lambda x : x**2
<function <lambda> at 0x030A76F0>
>>> lambda x : True if x % 2==0 else False
<function <lambda> at 0x030A7738>

>>> l = [[1,2],[2,3],[4,5]]
>>> l.sort(key=lambda x: x[1])
>>> print(l)
[[1, 2], [2, 3], [4, 5]]

'''
# 高阶函数
# map映射
'''
>>> l = [1,3,5]
>>> m = map(lambda x: x**2, [1,3,5])
# 获得返回结果是一个map对象
>>> m
<map object at 0x030A5C50>
>>> l
[1, 3, 5]
# map对象是一个可迭代对象, 需要驱动可迭代对象返回值, list就有这样的功能. 暂时不要太纠结
>>> list(m)
[1, 9, 25]

以上等同于
>>> def my_power_2(a):
...     return a**2
...
>>> m = map(my_power_2,[1,3,5])
>>> list(m)
[1, 9, 25]

>>> import math
>>> m = map(math.sqrt,[1,4,25,36])
>>> list(m)
[1.0, 2.0, 5.0, 6.0]

'''

# filter过滤
'''
>>> l = [1,0,4,5,8,9]
>>> f = filter(lambda x: x%2, l)
>>> list(f)
[1, 5, 9]
# 过滤奇数
>>> f = filter(lambda x: False if x%2 == 1 else True, l)
>>> list(f)
[0, 4, 8]

'''

# 递归函数

'''
>>> def fact(n):
...     if n == 1:
...         return 1
...     return n * fact(n-1)
...
>>> fact (5)
120

'''

# 课后作业
'''
给定一个列表, 根据grade来排序

classes = [
    {"name": "n_1", "age": 24, "grade": "A"},
    {"name": "n_2", "age": 23, "grade": "B"},
    {"name": "n_3", "age": 28, "grade": "A"},
    {"name": "n_4", "age": 24, "grade": "A"},
    {"name": "n_5", "age": 25, "grade": "C"},
    {"name": "n_6", "age": 21, "grade": "D"},
    {"name": "n_7", "age": 27, "grade": "A"},
]
通过filter语句来筛选出Grade为A的同学

通过map函数将上述同学的age + 1(对原数据结构有影响, 尽量不要用lambda)

使用递归函数重构斐波那契函数

f(n) = f(n-1) + f(n-2)
'''
classes = [
    {"name": "n_1", "age": 24, "grade": "A"},
    {"name": "n_2", "age": 23, "grade": "B"},
    {"name": "n_3", "age": 28, "grade": "A"},
    {"name": "n_4", "age": 24, "grade": "A"},
    {"name": "n_5", "age": 25, "grade": "C"},
    {"name": "n_6", "age": 21, "grade": "D"},
    {"name": "n_7", "age": 27, "grade": "A"},
]
l = classes
l.sort(key=lambda x: x["grade"])
print(l)
print("通过filter语句来筛选出Grade为A的同学")
f = filter(lambda x: x["grade"] == 'A',classes)
print(list(f))
print("通过map函数将上述同学的age + 1(对原数据结构有影响, 尽量不要用lambda)")
def add(l):
    l["age"]+=1
    return l
m = map(add,classes)
print(list(m))
print("使用递归函数重构斐波那契函数")
def fibs(n):
    if(n<2):
        return 1
    else:
        return fibs(n-2)+fibs(n-1)

for i in range(11):
    print(fibs(i),end=" ")